package com.security.door.controller.calendarManage;

import com.github.pagehelper.PageInfo;
import com.security.door.core.wrapper.WrapMapper;
import com.security.door.core.wrapper.Wrapper;
import com.security.door.model.domain.calendarAttendance;
import com.security.door.model.dto.recordAttendanceDto;
import com.security.door.service.IcalendarAttendanceService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.UUID;

@RestController
@Api(value = "WEB - recordDoorController", produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
public class calendarAttendanceController {
    @Autowired
    HttpSession session;
    @Autowired
    HttpServletRequest httpServletRequest;
    @Autowired
    IcalendarAttendanceService irecordAttendanceService;

    @PostMapping("/calendar/att/queryListWithPage")
    @ApiOperation(value = "分页查询所有信息", notes = "分页查询所有信息")
    public Wrapper<PageInfo<calendarAttendance>> listAll(@RequestBody recordAttendanceDto entityDto) {
        int pageIndex = httpServletRequest.getParameter("start") == null ? 0 : Integer.parseInt(httpServletRequest.getParameter("start"));
        int pageSize = httpServletRequest.getParameter("length") == null ? Integer.MAX_VALUE : Integer.parseInt(httpServletRequest.getParameter("length"));
        String searchStr = httpServletRequest.getParameter("search[value]");
        entityDto.setKeyWord(searchStr);
        entityDto.setPageIndex(pageIndex / pageSize + 1);
        entityDto.setPageSize(pageSize);
        var list = irecordAttendanceService.queryListWithPage(entityDto);
        return WrapMapper.ok(list);
    }

    @GetMapping("/calendar/att/amOn/{persionCode}")
    @ApiOperation(value = "上班打卡", notes = "上班打卡")
    public Wrapper amOn(@PathVariable("persionCode") String persionCode) throws Exception {
        SimpleDateFormat myFmt = new SimpleDateFormat("yyyy-MM-dd");
        var attenScheduleVo = irecordAttendanceService.selectByPerionCode(persionCode);
        DateFormat df = new SimpleDateFormat("HH:mm:ss");
        Date startTime = null;
        Date earliestTime = null;
        boolean isAm = true;
        //两次打卡
        if (null != attenScheduleVo.getAm_off() && null != attenScheduleVo.getPm_on()) {
            if (new Date().getTime() < df.parse(attenScheduleVo.getAm_off()).getTime()) {
                earliestTime = df.parse(attenScheduleVo.getAm_earliest());
                startTime = df.parse(attenScheduleVo.getAm_on());
                isAm = true;
            } else {
                earliestTime = df.parse(attenScheduleVo.getPm_earliest());
                startTime = df.parse(attenScheduleVo.getPm_on());
                isAm = false;
            }
        } else {
            earliestTime = df.parse(attenScheduleVo.getAm_earliest());
            startTime = df.parse(attenScheduleVo.getAm_on());
            isAm = true;
        }
        if (new Date().getTime() < earliestTime.getTime()) {
            WrapMapper.error("未到打卡时间");
        }
        calendarAttendance entity = irecordAttendanceService.selectByDate(myFmt.format(new Date()), persionCode);
        long lateTime = new Date(startTime.getTime() + attenScheduleVo.getLate_time() * 1000).getTime();
        long absentTime = new Date(startTime.getTime() + attenScheduleVo.getAbsent_time() * 1000).getTime();
        if (null == entity) {
            entity = new calendarAttendance();
            entity.setAttDate(new Date());
            entity.setPersioncode(persionCode);
            entity.setGuid(UUID.randomUUID().toString());
            if (isAm)
                entity.setAmOn(new Date());
            else
                entity.setPmOn(new Date());
            entity.setState(0);
            //迟到
            if (new Date().getTime() > lateTime) {
                entity.setState(1);
            }
            //缺席
            if (new Date().getTime() > absentTime) {
                entity.setState(2);
            }
            irecordAttendanceService.save(entity);
        } else {
            if (null == entity.getAmOn() || entity.getAmOn().toString().isBlank()) {
                entity.setAmOn(new Date());
                //迟到
                if (new Date().getTime() > lateTime) {
                    entity.setState(1);
                }
                //缺席
                if (new Date().getTime() > absentTime) {
                    entity.setState(2);
                }
            }
            irecordAttendanceService.update(entity);
        }
        return WrapMapper.ok();
    }

    @GetMapping("/calendar/att/pmOff/{persionCode}")
    @ApiOperation(value = "下班打卡", notes = "下班打卡")
    public Wrapper pmOff(@PathVariable("persionCode") String persionCode) throws Exception {
        SimpleDateFormat myFmt = new SimpleDateFormat("yyyy-MM-dd");
        var attenScheduleVo = irecordAttendanceService.selectByPerionCode(persionCode);
        DateFormat df = new SimpleDateFormat("HH:mm:ss");
        boolean isAm = false;
        //两次打卡
        if (null != attenScheduleVo.getAm_off() && null != attenScheduleVo.getPm_on()) {
            if (new Date().getTime() < df.parse(attenScheduleVo.getAm_off()).getTime()) {
                isAm = true;
            } else {
                isAm = false;
            }
        }
        calendarAttendance entity = irecordAttendanceService.selectByDate(myFmt.format(new Date()), persionCode);
        if (null == entity) {
            entity = new calendarAttendance();
            entity.setAttDate(new Date());
            entity.setPersioncode(persionCode);
            entity.setGuid(UUID.randomUUID().toString());
            if (isAm)
                entity.setAmOff(new Date());
            else
                entity.setPmOff(new Date());
            irecordAttendanceService.save(entity);
        } else {
            if (null == entity.getPmOff() || entity.getPmOff().toString().isBlank()) {
                if (isAm)
                    entity.setAmOff(new Date());
                else
                    entity.setPmOff(new Date());
            }
            irecordAttendanceService.update(entity);
        }
        return WrapMapper.ok();
    }
}
